import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Database {
	private static final String DATABASE_NAME = "BusTranserco.mdb";
	private static final String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
	private static final String accessDBURLSuffix = ";DriverID=22;READONLY=true}";
	private static final String sqlUrl = "jdbc:sqlserver://" + "localhost" + ":1433" +	";DatabaseName=Bus";
	static {
		try {
			// mdb
			//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			// M$ SQL
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			
		} catch(ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null, "Khong tim thay CSDL");
			System.exit(0);
		}
	}

	private static Connection conn = null;
	/** Creates a Connection to a Access Database */
	public static Connection getAccessDBConnection(){
		try {
		if (conn == null) {
			//Access mdb
			//String databaseURL = accessDBURLPrefix + DATABASE_NAME + accessDBURLSuffix;
			String databaseURL = sqlUrl;
			// System.err.println("Datebase URL: " + databaseURL);
			return DriverManager.getConnection(databaseURL, "sa", "123456");
		} else {
			return conn;
		}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Khong tim thay CSDL");
			System.exit(0);
			return null;
		}
	}
}
